import java.io.*;
import java.util.logging.Level;
import java.util.logging.Logger;

public class eg10_3 {
    public static void main(String[] args) {
        File helloFile = new File("F://", "Helloworld.txt");
        try {
            // 创建文件
            boolean isCreated = helloFile.createNewFile();
            if (isCreated || helloFile.exists()) {
                // 以字符流方式写入文件helloFile
                String content[] = {
                        "好雨知时节，当春乃发生.\n", "随风潜入夜，润物细无声.\n",
                        "野径云俱黑，江船火独明.\n", "晓看红湿处，花重锦官城.\n"
                };
                try (BufferedWriter bufferWriter = new BufferedWriter(new FileWriter(helloFile))) {
                    for (String str : content) {
                        bufferWriter.write(str);
                    }
                    bufferWriter.newLine();
                } // try-with-resources会自动关闭bufferWriter

                // 以字符流方式读出文件helloFile
                try (BufferedReader bufferReader = new BufferedReader(new FileReader(helloFile))) {
                    String s;
                    while ((s = bufferReader.readLine()) != null) {
                        System.out.println(s);
                    }
                } // try-with-resources会自动关闭bufferReader
            }
        } catch (IOException ex) {
            Logger.getLogger(eg10_3.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}